function jcb_email(to,subject,body,attachments)
% varriables for blat commands
pw='beaming2009';
from='masimulation1@gmail.com';

attachstr='';
if nargin>=4
    if iscellstr(attachments)
        for ii=1:length(attachments)
            attachstr = [attachstr ' -attach ' attachments{ii}];
        end
    else
        attachstr = [attachstr ' -attach ' attachments];
    end
end
if nargin<3
    body = '';
elseif exist(body,'file')~=2
    % if 'body' isn't a filename, it's some text, so insert the
    % appropriate switch & add quotes arround it
    body = ['-body ''' body ''''];
end
if nargin<2
    subject = '''an email sent from matlab''';
else
    % make sure it's surrounded with quotes
    if subject(1)~='''' || subject(1)~='"'
        subject = ['"' subject];
    end
    if subject(end)~=subject(1)
        subject = [subject subject(1)];
    end
end
if nargin<1
    to='jcb91@cam.ac.uk';
end

% send email about completion
blatcom=['blat' body ' -server 127.0.0.1 -to ' to ' -f ' from ' -u ' from ...
         ' -pw ' pw ' -subject ' subject attachstr];
system(blatcom);
